package com.ruoyi.store.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.store.domain.StoDish;

import com.ruoyi.store.mapper.StoDishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.store.mapper.StoShoppingCartMapper;
import com.ruoyi.store.domain.StoShoppingCart;
import com.ruoyi.store.service.IStoShoppingCartService;

/**
 * 购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-19
 */


@Service
public class StoShoppingCartServiceImpl implements IStoShoppingCartService 
{
    @Autowired
    private StoShoppingCartMapper stoShoppingCartMapper;


    @Autowired
    private StoDishMapper StoDishMapper;
    /**
     * 查询购物车
     * 
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public StoShoppingCart selectStoShoppingCartById(Long id)
    {
        return stoShoppingCartMapper.selectStoShoppingCartById(id);
    }

    /**
     * 查询购物车列表
     * 
     * @param stoShoppingCart 购物车
     * @return 购物车
     */
    @Override
    public List<StoShoppingCart> selectStoShoppingCartList(StoShoppingCart stoShoppingCart)
    {
        return stoShoppingCartMapper.selectStoShoppingCartList(stoShoppingCart);
    }

    /**
     * 新增购物车
     * 
     * @param stoShoppingCart 购物车
     * @return 结果
     */
    @Override
    public int insertStoShoppingCart(StoShoppingCart stoShoppingCart)
    {
        stoShoppingCart.setCreateTime(DateUtils.getNowDate());
        return stoShoppingCartMapper.insertStoShoppingCart(stoShoppingCart);
    }



    @Override
    public void insertStoShoppingCart2(StoShoppingCart stoShoppingCart) {

        Long userId = SecurityUtils.getUserId();
        stoShoppingCart.setUserId(userId);
        List<StoShoppingCart> list = stoShoppingCartMapper.selectStoShoppingCartList(stoShoppingCart);
        if (list!=null&&list.size()>0){
            StoShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            stoShoppingCartMapper.updateStoShoppingCart2(cart);
        }
        else {

            Long dishId = stoShoppingCart.getDishId();
            Long setmealId = stoShoppingCart.getSetmealId();
            if (dishId!=null&&dishId>0){
                StoDish dish = StoDishMapper.selectStoDishById(dishId);
                stoShoppingCart.setNumber(1L);
                stoShoppingCart.setImage(dish.getImage());
                stoShoppingCart.setName(dish.getName());
                stoShoppingCart.setAmount(dish.getPrice());
                stoShoppingCart.setCreateTime(DateUtils.getNowDate());
            }
            else if (setmealId!=null&&setmealId>0){

            }

            stoShoppingCartMapper.insertStoShoppingCart(stoShoppingCart);

        }

    }

    /**
     * 修改购物车
     * 
     * @param stoShoppingCart 购物车
     * @return 结果
     */
    @Override
    public int updateStoShoppingCart(StoShoppingCart stoShoppingCart)
    {
        return stoShoppingCartMapper.updateStoShoppingCart(stoShoppingCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteStoShoppingCartByIds(Long[] ids)
    {
        return stoShoppingCartMapper.deleteStoShoppingCartByIds(ids);
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteStoShoppingCartById(Long id)
    {
        return stoShoppingCartMapper.deleteStoShoppingCartById(id);
    }
}
